package com.dd.admin.business.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.common.exception.ApiException;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.author.entity.Author;
import com.dd.admin.business.author.mapper.AuthorMapper;
import com.dd.admin.business.author.service.AuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.admin.common.service.MinioService;
import com.dd.admin.common.utils.AddressUtils;
import com.dd.admin.common.utils.IPUtils;
import com.dd.admin.common.utils.RandomXiaohongshuAuthorName;
import com.dd.admin.common.utils.StringUtil;
import com.dd.admin.business.author.domain.AuthorAdminAddDto;
import com.dd.admin.business.file.service.FileService;
import com.dd.admin.common.util.MinioUrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dd.admin.business.author.domain.AuthorVo;
import com.dd.admin.business.author.domain.AuthorDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 作者（博主） 服务实现类
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-12-12
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author> implements AuthorService {

    @Autowired
    HttpServletRequest request;
    
    @Autowired
    private FileService fileService;

    @Autowired
    MinioService minioService;
    
    @Autowired
    private MinioUrlUtil minioUrlUtil;
    
    @Value("${server.port}")
    private String port;

    @Override
    public IPage<AuthorVo> selectAuthorPage(AuthorDto authorDto) {
        Page page = PageFactory.defaultPage();
        IPage<AuthorVo> result = baseMapper.selectAuthorPage(page,authorDto);
        
        // 刷新MinIO URL
        if (result != null && result.getRecords() != null) {
            result.getRecords().forEach(this::refreshAuthorUrls);
        }
        
        return result;
    }

    @Override
    public IPage<AuthorVo> selectAuthorList(AuthorDto authorDto) {
        Page page = PageFactory.defaultPage();
        IPage<AuthorVo> result = baseMapper.selectAuthorList(page,authorDto);
        
        // 刷新MinIO URL
        if (result != null && result.getRecords() != null) {
            result.getRecords().forEach(this::refreshAuthorUrls);
        }
        
        return result;
    }
    
    /**
     * 刷新作者的MinIO URL
     */
    private void refreshAuthorUrls(AuthorVo author) {
        if (author == null) return;
        
        // 刷新头像URL
        author.setAvatarUrl(minioUrlUtil.refreshMinioUrl(author.getAvatarUrl()));
        
        // 刷新背景图URL
        author.setBackGroundUrl(minioUrlUtil.refreshMinioUrl(author.getBackGroundUrl()));
    }

    @Override
    public Author selectAuthorByPhoneNumber(String phoneNumber) {
        return baseMapper.selectAuthorByPhoneNumber(phoneNumber);
    }

    @Override
    public Author createNewAuthor(String phoneNumber) {
        Author author = new Author();
        author.setPhoneNumber(phoneNumber);
        //随机生成昵称
        author.setAuthorName(RandomXiaohongshuAuthorName.generateAuthorName());
        author.setAuthorNo(StringUtil.getNumberForPK() + StringUtil.createCode(2));
        author.setDescription("我是" + author.getAuthorName());
        //生成随机头像因为没有点击上传所有没有头像id
        author.setAvatarUrl("http://8.146.211.120:8080/upload/avatar/avatar ("+ StringUtil.createCode(1) +").jpg");
        author.setBackGroundUrl("http://8.146.211.120:8080/upload/notes/note (6).jpg");
        author.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP
        author.setIpRealAddress(AddressUtils.getRealAddress(author.getIpAddress())); //ip真实地址
        this.save(author);
        return author;
    }

    @Override
    public Author createAuthorByAdmin(AuthorAdminAddDto authorDto) {
        if (selectAuthorByPhoneNumber(authorDto.getPhoneNumber()) != null) {
            throw new ApiException("该手机号已被注册");
        }
        // 检查昵称是否重复
        if (StringUtil.isNotEmpty(authorDto.getAuthorName())) {
            LambdaQueryWrapper<Author> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Author::getAuthorName, authorDto.getAuthorName());
            if (this.getOne(queryWrapper) != null) {
                throw new ApiException("该昵称已被使用");
            }
        }

        Author author = new Author();
        author.setPhoneNumber(authorDto.getPhoneNumber());

        author.setAuthorName(StringUtil.isNotEmpty(authorDto.getAuthorName()) ? authorDto.getAuthorName() : RandomXiaohongshuAuthorName.generateAuthorName());
        author.setDescription(StringUtil.isNotEmpty(authorDto.getDescription()) ? authorDto.getDescription() : "我是" + author.getAuthorName());
        author.setAuthorNo(StringUtil.getNumberForPK() + StringUtil.createCode(2));

        if (StringUtil.isNotEmpty(authorDto.getAvatarUrl())) {
            author.setAvatarUrl(authorDto.getAvatarUrl());
            author.setBackGroundUrl(authorDto.getAvatarUrl());
        } else {
            author.setAvatarUrl("http://8.146.211.120:8080/upload/avatar/avatar ("+ StringUtil.createCode(1) +").jpg");
            author.setBackGroundUrl("http://8.146.211.120:8080/upload/notes/note (6).jpg");
        }
        author.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP
        author.setIpRealAddress(AddressUtils.getRealAddress(author.getIpAddress())); //ip真实地址
        this.save(author);
        return author;
    }

    @Override
    public Long selectAuthorUpAndStarTotalCount(String authorId) {
        return baseMapper.selectAuthorUpAndStarTotalCount(authorId);
    }
}
